//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.BoxRent;
import com.aidong.model.wills.BoxRentInfo;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.PayList;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class BoxRentDao extends BaseDao<BoxRent, Integer> {
	public BoxRentDao() {
	}

	public List<BoxRent> getListByMemberCode(String memberCode) {
		return this.queryList("memberCode=:p1", new Object[]{memberCode}, (new OrderSet()).desc("saleDate"), -1);
	}

	public void queryRentPageList(Pagination<BoxRentInfo> pg, ConditionMap cd) {
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select c.shopCode,c.code boxCode,c.chName,c.deposit,c.yearFee,c.monthFee,c.useType,r.memberCode,m.chName memberName,r.beginTime,r.endTime,r.depositAmount,r.factAmount\nfrom BoxCode c left join (\nselect shopCode,boxCode,memberCode,min(beginTime) beginTime,max(endTime) endTime,sum(depositAmount) depositAmount,sum(factAmount) factAmount\nfrom BoxRent where returnDate is null group by shopCode,boxCode,memberCode\n) r on c.shopCode=r.shopCode and c.code=r.boxCode\nleft join MemberInfo m on r.memberCode=m.memberCode\nwhere c.status=1" + (sqlw.length() > 0?" and " + sqlw:"") + " order by c.shopCode,c.code";
		this.queryPageListBySql(BoxRentInfo.class, pg, sql, params.toArray());
	}

	public BoxRent getRentByCode(String shopCode, String boxCode) {
		String sql = "select shopCode,boxCode,memberCode,min(beginTime) beginTime,max(endTime) endTime,sum(depositAmount) depositAmount,sum(factAmount) factAmount\nfrom BoxRent where shopCode=:p1 and boxCode=:p2 and returnDate is null group by shopCode,boxCode,memberCode order by endTime desc";
		List list = this.queryListBySql(BoxRent.class, sql, new Object[]{shopCode, boxCode});
		return list.size() > 0?(BoxRent)list.get(0):null;
	}

	public List<BoxRent> getRentListByCode(String shopCode, String boxCode) {
		String sqlww = "shopCode=:p1 and boxCode=:p2 and returnDate is null";
		return this.queryList(sqlww, new Object[]{shopCode, boxCode}, (new OrderSet()).desc("endTime"), -1);
	}

	public Date getEndDateByMemberCode(String shopCode, String memberCode) {
		String sql = "select max(endTime) from BoxRent where shopCode=:p1 and memberCode=:p2 and returnDate is null";
		return (Date)this.getBySql(Date.class, sql, new Object[]{shopCode, memberCode});
	}

	public void addRent(BoxRent rent, List<PayList> payList) {
		this.add(rent);
		Iterator memberName = payList.iterator();

		while(memberName.hasNext()) {
			PayList sign = (PayList)memberName.next();
			sign.setSaleId(rent.getSaleId());
			sign.setContractCode(String.valueOf(rent.getSaleId()));
			this.getSession().save(sign);
			if("S011".equalsIgnoreCase(sign.getPayType())) {
				String sql = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
				this.updateBySql(sql, new Object[]{Integer.valueOf(2), sign.getAutoId(), sign.getSalesCode(), sign.getSalesCode2(), new Date(), sign.getNote()});
			}
		}

		String memberName1;
		if(rent.getEarnestId() != null) {
			memberName1 = "update MemberEarnest set status=1, useSaleId=:p1, useDate=getdate() where saleId=:p2";
			this.updateBySql(memberName1, new Object[]{rent.getSaleId(), rent.getEarnestId()});
		}

		memberName1 = (String)this.getBySql(String.class, "select chName from MemberInfo where memberCode=:p1", new Object[]{rent.getMemberCode()});
		ContractSign sign1 = new ContractSign();
		sign1.setShopCode(rent.getShopCode());
		sign1.setContractCode(String.valueOf(rent.getSaleId()));
		sign1.setContractType("08");
		sign1.setSaleId(rent.getSaleId());
		sign1.setMemberCode(rent.getMemberCode());
		sign1.setMemberName(memberName1 == null?"":memberName1);
		sign1.setStatus(Integer.valueOf(1));
		sign1.setSalesCode(rent.getSalesCode());
		sign1.setSalesCode2(rent.getSalesCode2());
		sign1.setCreateUser(rent.getCreateUser());
		sign1.setCreateTime(new Date());
		sign1.setUpdateTime(new Date());
		this.getSession().save(sign1);
	}

	public void updateReturn(List<BoxRent> rentList, List<PayList> payList) {
		String sql = "update BoxRent set returnDate=:p1, returnDeposit=:p2, addAmount=:p3, returnUser=:p4 where saleId=:p5";
		Iterator i$ = rentList.iterator();

		while(i$.hasNext()) {
			BoxRent pay = (BoxRent)i$.next();
			this.updateBySql(sql, new Object[]{pay.getReturnDate(), pay.getReturnDeposit(), pay.getAddAmount(), pay.getReturnUser(), pay.getSaleId()});
		}

		i$ = payList.iterator();

		while(i$.hasNext()) {
			PayList pay1 = (PayList)i$.next();
			this.getSession().save(pay1);
			if("S011".equalsIgnoreCase(pay1.getPayType())) {
				sql = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
				this.updateBySql(sql, new Object[]{Integer.valueOf(2), pay1.getAutoId(), pay1.getSalesCode(), pay1.getSalesCode2(), new Date(), pay1.getNote()});
			}
		}

	}
}
